package common.service.base.aop;

import org.apache.commons.lang3.StringUtils;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.reflect.MethodSignature;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

import common.service.base.annotation.NeedCheckSign;
import common.service.base.common.MobileSession;
import common.service.base.common.ResponseData;
import common.service.base.common.SessionInfo;
import common.service.contants.MobileConfig;
import common.service.util.MD5Util;
import common.service.util.ToolsUtil;

/**
 * @description 验签
 * @author zhangwenhan
 * @date 2018年11月13日 上午10:45:31
 */
@Component
@Aspect
public class SignAOP {

    @Autowired
    MobileSession mobileSession;

    private Logger logger = LoggerFactory.getLogger(SignAOP.class);

    /**
     * @description 使用环绕AOP 在进入方法之前进行处理，方法后不进行处理
     * @version 1.0.0
     * @author zhangwenhan
     * @date 2018年11月13日 上午11:03:28
     * @param pjp
     * @return
     * @throws Throwable
     */
    @Around("execution(public * *.service..web.*.*(..))")
    public Object aroundParamValidReturn(ProceedingJoinPoint pjp) throws Throwable {
        logger.debug("进入验签 aroundParamValidReturn");
        MethodSignature signature = (MethodSignature) pjp.getSignature();
        if (signature.getMethod().getAnnotation(NeedCheckSign.class) == null) {
            return pjp.proceed();
        }
        Object[] objs = pjp.getArgs();
        String reqStr = JSON.toJSONString(objs[0]);
        JSONObject req = JSON.parseObject(reqStr);
        if (checkSign(req)) {
            return pjp.proceed();
        }
        return ResponseData.fail("验签失败", null);
    }

    private boolean checkSign(JSONObject req) {
        try {
            String sign = req.getString("sign");
            if (StringUtils.isBlank(sign)) {
                return false;
            }
            String md5Key = "";
            SessionInfo sessionInfo = (SessionInfo) mobileSession.getAttribute(MobileConfig.SESSION_USR_INFO);
            if (sessionInfo != null) {
                md5Key = MD5Util.md5Digest(sessionInfo.getUserId().toString() + sessionInfo.getCreateTime());
            }
            String reqTime = req.getString("reqTime");
            String data = req.getString("data");
            String signStr = ToolsUtil.getMD5(data + reqTime + md5Key);
            if (signStr.equals(sign)) {
                return true;
            }
            logger.debug("验签失败,req:{}, md5Key:{}, signStr:{}", req.toJSONString(), md5Key, signStr);
            return false;
        } catch (Exception e) {
            logger.error("验签异常", e);
            return false;
        }
    }
    
    public static void main(String[] args) throws Exception {
        String data ="{\"assetId\":1223,\"taskId\":\"35d10ca973fda8e89e552d9708e744a5\"}";
        String reqTime = "1542598397640";
        String md5Key = MD5Util.md5Digest("16" + "20181115112727");
        String signStr = ToolsUtil.getMD5(data + reqTime + md5Key);
        System.out.println(signStr);
        String sign = "fa4526ddb72e84e6815d8155585bf681";
        System.out.println("10499f113d89694bcf5567c81259855b".length());
        System.out.println(sign.length());
    }
    
}
